<template>
  <div class="login">
    <div class="login-from">
        <div class="login-img">
            <img src="../asstas/logo.png" alt="" />
        </div>
        <el-form ref="loginformref" :model="loginform" :rules="loginFormRules">
            <el-form-item prop="username">
                <el-input v-model="loginform.username" placeholder="请输入用户名" prefix-icon="iconfont icon-user"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="loginform.password" type='password' placeholder="请输入密码" prefix-icon="iconfont icon-3702mima"></el-input>
            </el-form-item>
            <el-form-item class="btns">
                <el-button type="primary" @click='login()'>登陆</el-button>
                <el-button type="info"  @click='chongzhi()'>重置</el-button>
            </el-form-item>
        </el-form>
    </div>
  </div>
</template>

<script>
export default {
    data() {
        return {
            loginform:{
                username:'',
                password:''
            },
            loginFormRules:{
                username: [
                    { required: true, message: '请输入登录名', trigger: 'blur' },
                    {
                        min: 3,
                        max: 10,
                        message: '登录名长度在 3 到 10 个字符',
                        trigger: 'blur'
                    }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    {
                        min: 6,
                        max: 15,
                        message: '密码长度在 6 到 15 个字符',
                        trigger: 'blur'
                    }
                ]
            }
        }
    },
    methods: {
        login:function(){
           this.$refs.loginformref.validate(async (valid)=>{
               if(valid===true){
                   var { data: res } = await this.$http.post('/login', this.loginform)
                   if(res.meta.status===200){
                        this.$message({
                        message: '恭喜你，登陆成功!',
                        type: 'success'
                        });
                        // 添加令牌到本地存储
                        window.sessionStorage.setItem('token', res.data.token)
                        // 跳转网页
                        this.$router.push('/Home')
                   }else{
                       this.$message({
                        message: '登陆失败,用户名或密码不正确',
                        type: 'error'
                        });
                   }
               }else{
                   this.$message.error('登陆错误,请检查用户名或密码是否正确!');
               }
           })
        },
        chongzhi:function(){
            this.loginform.username=''
            this.loginform.password=''
        }
    },
}
</script>

<style lang="less" scoped>
.login {
  height: 100%;
  background-color: #2b4b6b;
  position: relative;
  .login-from {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 10px;
    box-shadow: 0 0 10px rgba(255,255,255,.5);
    .login-img img {
      box-shadow: 0 0 10px rgba(255,255,255,.5);
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      width: 152px;
      height: 152px;
      border-radius: 50%;
      background-color: #fff;
      overflow: hidden;
      padding: 10px;
    }
    .el-form{
        width: 100%;
        position: absolute;
        bottom: 0;
        padding: 20px;
        .btns{
            text-align: right;
        }
    }
  }
}
</style>